package com.alibaba.otter.manager.web.resource;

import com.alibaba.otter.manager.biz.config.pipeline.PipelineService;
import com.alibaba.otter.manager.web.dto.RestResponse;
import com.alibaba.otter.shared.arbitrate.ArbitrateViewService;
import com.alibaba.otter.shared.arbitrate.model.PositionEventData;
import com.alibaba.otter.shared.common.model.config.pipeline.Pipeline;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 位点信息查询
 */
@RequestMapping("/api/v1/position")
@RestController
public class PositionResource {


    private static final Logger logger = LoggerFactory.getLogger(PositionResource.class);

    private final PipelineService pipelineService;

    private final ArbitrateViewService arbitrateViewService;

    public PositionResource(PipelineService pipelineService, ArbitrateViewService arbitrateViewService) {
        this.pipelineService = pipelineService;
        this.arbitrateViewService = arbitrateViewService;
    }


    @GetMapping("/remove/{pipelineId}")
    public RestResponse<Void> remove(@PathVariable("pipelineId") Long pipelineId) throws Exception {
        Pipeline pipeline = pipelineService.findById(pipelineId);
        String destination = pipeline.getParameters().getDestinationName();
        short clientId = pipeline.getParameters().getMainstemClientId();
        PositionEventData position = arbitrateViewService.getCanalCursor(destination, clientId);
        logger.warn("remove pipelineId[{}] position \n {}", pipelineId, position); // 记录一下日志
        arbitrateViewService.removeCanalCursor(destination, clientId);
        return RestResponse.ok();
    }
}
